package com.conversationboard.view;

import java.sql.SQLException;
import java.util.List;

import com.conversationboard.formatter.ImageHider;
import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.SearchResult;
import com.conversationboard.model.User;

public class SearchResultsDisplay {

	public SearchResultsDisplay() {
	}


	public static String display(List<SearchResult> results, User user) throws SQLException {

		StringBuilder buffer = new StringBuilder();

		for (SearchResult result : results) {

			Board board = Boards.getBoard(result.getBoardId());

			/* Board shouldn't be null, but belt and braces */

			if (board == null) {
				continue;
			}

			/* If search results are part of a private board that you're not a member of, ignore them. This will screw up the number of search
			 * results that are supposed to be returned, but it's unlikely to have a big impact. */

			if (board.isPrivateBoard()) {
				if (!user.isMemberOfBoard(result.getBoardId())) {
					continue;
				}
			}

			buffer.append("<div class=\"thread\"><a href='ThreadSelectedControllerServlet?boardid=" + result.getBoardId() + "&amp;threadid=" + result.getThreadId() + "'>" + "<strong>" + board.getName() + "</strong> " + result.getThreadTitle() + "</a></div>");

			if (result.getBody() != null) {
				String body = ImageHider.substituteImagesForLinks(result.getBody(), user.isAdmin(), false, !user.getPreferences().isShowImages());
				buffer.append(body);
			}

			ThreadListAdminLinksDisplay.includeAdminLinks(buffer, result.getBoardId(), result.getThreadId(), user, "adminlinks", "adminlink");
		}

		return buffer.toString();

	}

}
